﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevTeam;


using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AddKomp : XForm
    {
        public AddKomp()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void AddKomp_Load(object sender, EventArgs e)
        {
            
            TAM.HammaddeTableAdapter = new Datasets.MyDataTableAdapters.HammaddeTableAdapter();
            TAM.HammaddeTableAdapter.Fill(ds.Hammadde);

            bs = new BindingSource(ds, "Hammadde");
            InitGridView(gridView1, bs);
            gridView1.Columns["idx"].Visible = false;
            gridView1.AddSummary("Yuzdesi");
            gridView1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gridView1_RowCellStyle);

            btnEkle.ItemClick += btnEkle_ItemClick;
            bs.CurrentChanged += new EventHandler(bs_CurrentChanged);
            btnEkle.Enabled = false;
            gridView1.BestFitColumns();
        }

        void btnEkle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ds.Hammadde.Compute("Sum(Yuzdesi)", string.Empty).ToInt() != 100)
                XtraMessageBox.Show("Bileşenlerin toplamı %100 olmalıdır.");
            else
            {
                ExecuteNonQuery("UpdKompozisyon", ds.Hammadde.Serialize());
                AppSession.MainForm.RefreshData();
                XtraMessageBox.Show("Kompozisyon başarıyla kaydedildi.", "Yeni Kompozisyon", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        void bs_CurrentChanged(object sender, EventArgs e)
        {
            btnEkle.Enabled = ((ds.Hammadde.Compute("Sum(Yuzdesi)", string.Empty)).ToDecimal() == 100);            
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (gridView1.GetRowCellDisplayText(e.RowHandle, "Yuzdesi") != string.Empty)
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }

    }
}